﻿@if (AuthorizedState==1)
{
    @Authorizing
}
@if (AuthorizedState==2)
{
    @Authorized
}
@if (AuthorizedState==3)
{
    @NotAuthorized
}

@code {

    [Inject]
    private IUserAuthStateProvider userAuthStateProvider { set; get; }

    [Inject]
    protected NavigationManager _navigationManager { set; get; }

    private int AuthorizedState { set; get; }

    protected override void OnInitialized()
    {
        AuthorizedState=0;
        base.OnInitialized();
    }


    [Parameter]
    public RenderFragment Authorized { get; set; }

    [Parameter]
    public RenderFragment Authorizing { get; set; }

    [Parameter]
    public RenderFragment NotAuthorized { get; set; }


    protected override async Task OnAfterRenderAsync(bool firstRender)
    {

        if (AuthorizedState==0 || AuthorizedState==3)
        {
            AuthorizedState=1;
            await InvokeAsync(StateHasChanged);
            var authenticationState = await userAuthStateProvider.GetAuthenticationStateAsync();
            if (authenticationState)
            {
                AuthorizedState=2;
            }
            else
            {
                AuthorizedState=3;
                _navigationManager.NavigateTo("/login");
            }
            await InvokeAsync(StateHasChanged);


        }
        await base.OnAfterRenderAsync(firstRender);
    }


}
